{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e0d3790c",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1. while循环：while True 无限循环\n",
    ">> 1. 指定次数，while Ture 可以实现for循环的指定次数，增加一个count计数器即可。\n",
    ">> 2. while True 一般与用户输入一起使用，让用户可以重复输入，知道某些条件成立结束输入(购物车）\n",
    "> 2. 购物车实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "147f41ca",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 1. 元组\n",
    "> 2. 集合\n",
    "> 3. 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72354cf3",
   "metadata": {},
   "source": [
    "## 1.元组\n",
    "> 1. 元组的基本内容（形态，定义）\n",
    "> 2. 元组的性质\n",
    ">> 1. 有序性(indexing,sliceing)\n",
    ">> 2. <font color=\"red\">**不可变性**</font>,元组没有：增，删，改\n",
    "> 3. 元组的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4a23d033",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1800001, 1800002, 1800003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# 创建一个元组方法1\n",
    "网三_学号 = (1800001,1800002,1800003)\n",
    "print(网三_学号,type(网三_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2ed1ebfb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2110130098, 2110130093, 2110130097)\n"
     ]
    }
   ],
   "source": [
    "网二_学号 = (2110130098,2110130093,2110130097)\n",
    "print(网二_学号)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cd0f30a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1800001, 1800002, 1800003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# 创建一个元组方法2\n",
    "网三_学号 = 1800001,1800002,1800003\n",
    "print(网三_学号,type(网三_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d6fd1a22",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2110130093, 2110130097, 2110130096) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网二_学号 = 2110130093,2110130097,2110130096\n",
    "print(网二_学号,type(网二_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "64a0910f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2110130093"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组切片\n",
    "网二_学号[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c287ea7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2110130097"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d95593ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2110130097, 2110130096)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5f2676db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2110130093,)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6c453d1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=25, tm_hour=15, tm_min=7, tm_sec=9, tm_wday=1, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组的实际案例\n",
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2a3084c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on struct_time object:\n",
      "\n",
      "class struct_time(builtins.tuple)\n",
      " |  struct_time(iterable=(), /)\n",
      " |  \n",
      " |  The time value as returned by gmtime(), localtime(), and strptime(), and\n",
      " |  accepted by asctime(), mktime() and strftime().  May be considered as a\n",
      " |  sequence of 9 integers.\n",
      " |  \n",
      " |  Note that several fields' values are not the same as those defined by\n",
      " |  the C language standard for struct tm.  For example, the value of the\n",
      " |  field tm_year is the actual year, not year - 1900.  See individual\n",
      " |  fields' descriptions for details.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      struct_time\n",
      " |      builtins.tuple\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Helper for pickle.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  tm_gmtoff\n",
      " |      offset from UTC in seconds\n",
      " |  \n",
      " |  tm_hour\n",
      " |      hours, range [0, 23]\n",
      " |  \n",
      " |  tm_isdst\n",
      " |      1 if summer time is in effect, 0 if not, and -1 if unknown\n",
      " |  \n",
      " |  tm_mday\n",
      " |      day of month, range [1, 31]\n",
      " |  \n",
      " |  tm_min\n",
      " |      minutes, range [0, 59]\n",
      " |  \n",
      " |  tm_mon\n",
      " |      month of year, range [1, 12]\n",
      " |  \n",
      " |  tm_sec\n",
      " |      seconds, range [0, 61])\n",
      " |  \n",
      " |  tm_wday\n",
      " |      day of week, range [0, 6], Monday is 0\n",
      " |  \n",
      " |  tm_yday\n",
      " |      day of year, range [1, 366]\n",
      " |  \n",
      " |  tm_year\n",
      " |      year, for example, 1993\n",
      " |  \n",
      " |  tm_zone\n",
      " |      abbreviation of timezone name\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  n_fields = 11\n",
      " |  \n",
      " |  n_sequence_fields = 9\n",
      " |  \n",
      " |  n_unnamed_fields = 0\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(self, key, /)\n",
      " |      Return self[key].\n",
      " |  \n",
      " |  __getnewargs__(self, /)\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  count(self, value, /)\n",
      " |      Return number of occurrences of value.\n",
      " |  \n",
      " |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      " |      Return first index of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(time.localtime())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4b158a13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'> 2746501319744\n"
     ]
    }
   ],
   "source": [
    "# 元组里面可以包含列表\n",
    "tuple_test = ([1,2,3,4],[3,4,5,6])\n",
    "print(tuple_test,type(tuple_test),id(tuple_test)) # id 查找存储空间地址 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4e5ef726",
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple_test[0].append(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "822e6189",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 2, 3, 4, 100], [3, 4, 5, 6])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3836da2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2746501319744"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(tuple_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f83a2561",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一般情况下，元组里面存放的是常量(不可变)，何为常量：数字，字符串\n",
    "#                               不存放变量 如：列表，字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b77902d8",
   "metadata": {},
   "source": [
    "## 课本132"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fc6bf47f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', 'e', 'i', 'o', 'u') <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# 元组定义字符串\n",
    "vowels = (\"a\",\"e\",\"i\",\"o\",\"u\")\n",
    "print(vowels,type(vowels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "932b4a07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', 'e', 'i', 'o', 'u') <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "vowels = \"a\",\"e\",\"i\",\"o\",\"u\"\n",
    "print(vowels,type(vowels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "aa08e9e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'i'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8a3aafe6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python <class 'str'>\n",
      "('python',) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# 每个元组在小括号之间至少要包括一个小逗号，即使这个元组中只包含一个对象也不例外\n",
    "t = (\"python\")\n",
    "t1 = (\"python\",)\n",
    "print(t,type(t))\n",
    "print(t1,type(t1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3709807",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果你的数据中的数据从不改变，可以把这些数据放在一个元组中"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9408ddd9",
   "metadata": {},
   "source": [
    "## 2.集合(sets)\n",
    "> 1. 集合的基本概念\n",
    ">> 1. 性质： 没有重复项\n",
    ">> 2. 性质： 无序性\n",
    "> 2. 集合的一般方法\n",
    ">> 1. 集合的运算：&,|\n",
    ">> 2. 集合的一般方法：intersection\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "87558fbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'orange', 'apple', 'pear', 'orange', 'banana']\n",
      "{'banana', 'apple', 'pear', 'orange'}\n"
     ]
    }
   ],
   "source": [
    "# 没有重复项\n",
    "basker_lists = [\"apple\",\"orange\",\"apple\",\"pear\",\"orange\",\"banana\"]\n",
    "basker_sets = {\"apple\",\"orange\",\"apple\",\"pear\",\"orange\",\"banana\"}\n",
    "print(basker_lists)\n",
    "print(basker_sets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "89ca468a",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {1801,1802,1803,1804,1805}\n",
    "电竞社 = {1801,1804,1805,1806,1808}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5aaa9aac",
   "metadata": {},
   "source": [
    "* 集合的与运算，交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "566a7076",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1804, 1805}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 & 电竞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "5dfcb3a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1804, 1805}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.intersection(电竞社)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e5d91b9",
   "metadata": {},
   "source": [
    "* 集合的或运算，并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9065b440",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1804, 1805, 1806, 1808}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 | 电竞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "33130f61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# len查找个数\n",
    "len(舞蹈社 | 电竞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f6c72cbb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1804, 1805, 1806, 1808}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.union(电竞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "340c6694",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = {\"a\",\"b\",\"c\",\"d\"}\n",
    "t1 = {\"c\",\"d\",\"e\",\"f\",\"g\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cb2740ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'c', 'd'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交集\n",
    "t & t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "730f9ec1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'b', 'c', 'd', 'e', 'f', 'g'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 并集\n",
    "t | t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ee640831",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'b'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 差集\n",
    "t.difference(t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "bb0c8439",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'f', 'g'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1.difference(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38bf382e",
   "metadata": {},
   "source": [
    "## 课本123"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "bcf92550",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = {\"a\",\"e\",\"e\",\"i\",\"o\",\"u\",\"u\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8b55638d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cff784c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a eord to search for vowels:i\n",
      "i\n"
     ]
    }
   ],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"] # 创建一个列表\n",
    "word = input(\"Provide a eord to search for vowels:\")\n",
    "found = [] # 创建空列表\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cc477aae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'e', 'i', 'o', 'u']\n"
     ]
    }
   ],
   "source": [
    "# 从序列创建列表\n",
    "vowels2 = set(\"aeeiouu\") # 简写形式 向序列set函数传递任何序列\n",
    "print(vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "45720c2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:i\n",
      "i\n"
     ]
    }
   ],
   "source": [
    "# 练习\n",
    "vowels = set(\"aeiou\") # 创建集合\n",
    "word = input(\"Provide a word to search for vowels:\") # 输入\n",
    "found = vowels & set(word) # 交集\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "544aaebb",
   "metadata": {},
   "source": [
    "##  3.字典 (dict)\n",
    "\n",
    "> 1. 字典的基本概念：\n",
    ">> 1.字典性质：\n",
    ">>> 1. 无序性\n",
    ">>> 2. keys 是不可变的:就像列表、字符串等有序的数据结构的\n",
    ">>> 3. keys 是唯一的\n",
    ">> 2. 字典的形态\n",
    "> 2. 字典的一般方法\n",
    ">> 1. A字典的创建\n",
    ">> 2. B.字典的函数和方法\n",
    ">>> 1. 增加\n",
    ">>> 2. 删除\n",
    ">>> 3. 修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "43509187",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新二班 = {\n",
    "    1800001:\"李一锋\",\n",
    "    1800002:\"jack\",\n",
    "    1800003:\"marrk\",\n",
    "    1800004:\"周杰伦\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3b347e03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: '李一锋', 1800002: 'jack', 1800003: 'marrk', 1800004: '周杰伦'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(网新二班,type(网新二班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "00cd5e81",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 因为学号是唯一的，可以作为字典的key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "31c676c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: '李一锋', 1800002: 'jack', 1800003: 'marrk', 1800004: '周杰伦'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "网新二班 = {\n",
    "    1800001:\"李一锋\",\n",
    "    1800002:\"jack\",\n",
    "    1800003:\"marrk\",\n",
    "    1800004:\"周杰伦\"\n",
    "}\n",
    "print(网新二班,type(网新二班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "5bb27433",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'marrk'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典的取值：通过keys 进行字典的取值，unlike列表、元组和字符串通过index取值\n",
    "网新二班[1800003]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "6f9a267c",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新二班 = {\n",
    "    1800001:[\"李一锋\",\"广州\",\"男\",\"19\",\"聊天记录\",],\n",
    "    1800002:\"jack\",\n",
    "    1800003:\"marrk\",\n",
    "    1800004:\"周杰伦\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "06a46153",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李一锋', '广州', '男', '19', '聊天记录']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新二班[1800001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14d476e0",
   "metadata": {},
   "source": [
    "## 3.2字典的一般方法\n",
    "* 1.按照字典的形式直接创建(key:value)\n",
    "```\n",
    "网新二班 = {\n",
    "    1800001:\"李一峰\",\n",
    "    1800002:\"Jack\",\n",
    "    1800003:\"Marry\",\n",
    "    1800004:\"周杰伦\"\n",
    "}\n",
    "```\n",
    "* 2. 使用dict()方法创建字典\n",
    "```\n",
    "网新四班 = dict(\n",
    "    stu1800001=\"李一锋\",\n",
    "    stu1800002=\"jack\",\n",
    "    stu1800003=\"marry\",\n",
    "    stu1800004=\"周杰伦\",\n",
    ")\n",
    "```\n",
    "* 3. 使用字典增加的方式创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "ccdc6ec8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用dict()方法创建字典\n",
    "网新四班 = dict(\n",
    "stu1800001=\"李一锋\",\n",
    "stu1800002=\"jack\",\n",
    "stu1800003=\"marry\",\n",
    "stu1800004=\"周杰伦\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c6803edd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一锋',\n",
       " 'stu1800002': 'jack',\n",
       " 'stu1800003': 'marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新四班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "daa16f73",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用字典增加的方式创建字典 ： dict[key] = value\n",
    "网新一班 = {}\n",
    "网新一班[\"stu1800001\"] = \"李一锋\"\n",
    "网新一班[\"stu1800002\"] = \"jack\"\n",
    "网新一班[\"stu1800003\"] = \"marrk\"\n",
    "网新一班[\"stu1800004\"] = \"周杰伦\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "6e71f56b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一锋',\n",
       " 'stu1800002': 'jack',\n",
       " 'stu1800003': 'marrk',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "24d080ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新一班_keys = [\"stu1800001\",\"stu1800002\",\"stu1800003\",\"stu1800004\"] # 定义一个列表\n",
    "网新一班_values = [\"李一锋\",\"jack\",\"marry\",\"周杰伦\"] # 定义一个列表\n",
    "网新一班 = {} # 定义一个字典\n",
    "for i in range(4):\n",
    "    网新一班[网新一班_keys[i]] = 网新一班_values[i] # dict[key] = value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "02ca2133",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一锋',\n",
       " 'stu1800002': 'jack',\n",
       " 'stu1800003': 'marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4452a753",
   "metadata": {},
   "source": [
    "## 课本96"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "ea7e6160",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 这些数据有两列，也可以有多列\n",
    "person3 = {\"name\":\"Ford Prefect\",  # 键/值对\n",
    "          \"Gender\":\"Male\",\n",
    "          \"Occupation\":\"Researcher\",\n",
    "          \"Home Planet\":\"Betelgeuse Seven\"}\n",
    "# 想象成一个查找表，左边的键用来查找右边的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "6e6bf9ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Ford Prefect',\n",
       " 'Gender': 'Male',\n",
       " 'Occupation': 'Researcher',\n",
       " 'Home Planet': 'Betelgeuse Seven'}"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "70e9c574",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Ford Prefect'"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用键来访问字典中的数据 用中括号查找值\n",
    "person3[\"name\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b4257eb",
   "metadata": {},
   "source": [
    "# 课后作业："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c6d95941",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 直接创建字典\n",
    "网新二班 = {\n",
    "    2110130098:\"周杰伦\",\n",
    "    2110130097:\"marrk\",\n",
    "    2110130095:\"吴一凡\",\n",
    "    2110130092:\"李一锋\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "f1f65944",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2110130098: '周杰伦', 2110130097: 'marrk', 2110130095: '吴一凡', 2110130092: '李一锋'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(网新二班,type(网新二班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "88c2842c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# dict创建列表\n",
    "网新二班 = dict(\n",
    "stu2110130093 = \"周杰伦\",\n",
    "stu2110130094 = \"marrk\",\n",
    "stu2110130092 = \"吴一凡\",\n",
    "stu2110130091 = \"李一锋\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "3244f0c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2110130093': '周杰伦', 'stu2110130094': 'marrk', 'stu2110130092': '吴一凡', 'stu2110130091': '李一锋'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(网新二班,type(网新二班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c26260f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu2110130093': '周杰伦',\n",
       " 'stu2110130094': 'marrk',\n",
       " 'stu2110130092': '吴一凡',\n",
       " 'stu2110130091': '李一锋'}"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新二班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "6e6945e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# dict[key] = value\n",
    "网新二班_list = [2110130098,2110130096,2110130094,2110130099]\n",
    "网新二班_name = [\"周杰伦\",\"marrk\",\"吴一凡\",\"李一锋\"]\n",
    "网新二班 = {}\n",
    "for i in range(4):\n",
    "    网新二班[网新二班_list[i]] = 网新二班_name[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "72f7d514",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2110130098: '周杰伦', 2110130096: 'marrk', 2110130094: '吴一凡', 2110130099: '李一锋'}"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新二班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "00feb7cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "2e6ee20d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典的增加\n",
    "网新二班 = {\n",
    "    2110130098:\"周杰伦\",\n",
    "    2110130097:\"marrk\",\n",
    "    2110130095:\"吴一凡\",\n",
    "    2110130092:\"李一锋\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "4fc36029",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2110130098: '周杰伦', 2110130097: 'marrk', 2110130095: '吴一凡', 2110130092: '李一锋'}"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新二班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "c0d9e35b",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新二班[2110130091] = \"罗志祥\" # 将一个对象赋至一个新键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "32d5ba68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2110130098: '周杰伦',\n",
       " 2110130097: 'marrk',\n",
       " 2110130095: '吴一凡',\n",
       " 2110130092: '李一锋',\n",
       " 2110130091: '罗志祥'}"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新二班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79d8f720",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
